package com.dyh.quickdemozwei.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpPost;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;

@Controller
@RequestMapping("/param")
public class ParamController {

//    @RequestMapping("/route")
//    public Object route(HttpServletRequest httpServletRequest,@RequestBody JSONObject jo){
//
//        String requestURI = httpServletRequest.getRequestURI();
//        System.out.println(requestURI);
//
//        return "1";
//    }

    @RequestMapping("/diffHttp/{x}")
//    @ResponseBody
    public Object diffHttp(@PathVariable String x){
        if (x.length()>2){
            return new ResponseEntity<>("Invalid input", HttpStatus.BAD_REQUEST);
        }
        else {
            return new ResponseEntity<>("Success", HttpStatus.OK);

        }
    }

    @RequestMapping("/route")
//    @ResponseBody
    public Object route(HttpServletRequest httpServletRequest) throws IOException {

        String requestURI = httpServletRequest.getRequestURI();
        System.out.println(requestURI);

        InputStream in = httpServletRequest.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
        String content = IOUtils.toString(reader);

        System.out.println(content);


        HttpPost httpPost = new HttpPost("http://123123");

        Enumeration<String> element = httpServletRequest.getHeaderNames();
        while (element.hasMoreElements()) {
            String name = element.nextElement();
//            if (NEED_REMOVE_HEADERS.contains(StringUtils.lowerCase(name))) {
//                continue;


//            }
            String value = httpServletRequest.getHeader(name);
            httpPost.setHeader(name, value);
        }

        return JSON.toJSONString("{\"endUpdatedAt\":1693833301873,\"orderStatus\":5,\"page\":1,\"pageSize\":100,\"refundStatus\":5,\"startUpdatedAt\":1693833232000}\n");


    }



}
